package com.oa.oaspcpersonalcenter.controller;


import com.oa.oaspccommon.dto.ActionResult;
import com.oa.oaspccommon.dto.UserInfo;
import com.oa.oaspccommon.po.User;
import com.oa.oaspccommon.util.JwtUtil;
import com.oa.oaspcpersonalcenter.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author Sdx
 * 2021-04-15
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ActionResult login(@RequestBody UserInfo userInfo) {
        ActionResult result = null;
//       String checkPass=new Md5Hash(userInfo.getPassword(),"oasystem",1024).toString();
        String checkPass = userInfo.getPassword();//未加密
        User user = userService.findUserByUsername(userInfo.getUsername());
        if (user == null || !checkPass.equals(user.getPassword())) {
            return result = new ActionResult(400, "login false", null);
        }
        //登录成功 0为用户 1为管理
        if (user.getPassword().equals(checkPass) && user.getJurisdictiongroup().equals("0")) {
            String token = JwtUtil.createToken(user.getUsername());
            result = new ActionResult(200, "user login success", token);

        } else if (user.getPassword().equals(checkPass) && user.getJurisdictiongroup().equals("1")) {
            String s = JwtUtil.createToken(user.getUsername());
            result = new ActionResult(300, "admin login success", s);
        }
        return result;
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<User> findUsers() {
        List<User> users = userService.findUsers();
        return users;
    }

//    @GetMapping("/user/{uid}")
    @RequestMapping(value = "/user/{uid}", method = RequestMethod.GET)
    public User findUserByUid(@PathVariable("uid") int uid) {
        User user = userService.findUserByUid(uid);
        return user;
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public boolean addUser(@RequestBody User user) {

        boolean b = userService.addUser(user);
        return b;
    }

    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public boolean updateUser(@RequestBody User user) {
        boolean b = userService.updateUser(user);
        return b;
    }

    @RequestMapping(value = "/user", method = RequestMethod.DELETE)
    public boolean deleteUserByUid(@RequestParam(value = "uid") int uid) {
        boolean b = userService.deleteUserByUid(uid);
        return b;
    }

    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public ActionResult changePassword(@RequestBody UserInfo userInfo) {
        ActionResult result = null;

        String Pass = userInfo.getPassword();
        User user = userService.changePassword(userInfo.getPassword());
        if (user == null || !Pass.equals(user.getPassword())) {
            return result = new ActionResult(400, "用户或密码错误", null);
        } else if (user.getPassword().equals(Pass) && user.getUsername().equals(userInfo.getUsername())) {

            result = new ActionResult(300, "密码修改成功", "");
        }
        return result;
    }

}
